package cn.lopr.sccs;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shuangyang7
 * @date 2020/11/25 13:09
 */
public class Properties {

    protected static Map<String, Properties> propsMap = new HashMap<>();
    private java.util.Properties properties = new java.util.Properties();
    private String name;
    private int source;

    public Properties(String name) {
        this.name = name;
        this.properties = ResourceHelper.load(this.name);
        if (this.properties == null) {
            // TODO
            // jar file
            // zookeeper
            // TODO
            // user jar
        } else {
            this.source = 0;
        }
    }

    public static Properties loadProperties() {
        return loadProperties("default");
    }


    public static Properties loadProperties(String name) {
        name = name.trim();
        if (!propsMap.containsKey(name)) {
            synchronized (propsMap) {
                if (!propsMap.containsKey(name)) {
                    Properties properties = new Properties(name);
                    propsMap.put(name, properties);
                }
            }
        }
        return propsMap.get(name);
    }

    public static String getProperty(String key) {
        Properties props = loadProperties();
        return props.getValue(key);
    }

    public static String getProperty(String key, String defaultValue) {
        Properties props = loadProperties();
        return props.getValue(key, defaultValue);
    }

    public static boolean getProperty(String key, boolean defaultValue) {
        Properties props = loadProperties();
        return props.getValue(key, defaultValue);
    }

    public static byte getProperty(String key, byte defaultValue) {
        Properties props = loadProperties();
        return props.getValue(key, defaultValue);
    }

    public static short getProperty(String key, short defaultValue) {
        Properties props = loadProperties();
        return props.getValue(key, defaultValue);
    }

    public static int getProperty(String key, int defaultValue) {
        Properties props = loadProperties();
        return props.getValue(key, defaultValue);
    }

    public static long getProperty(String key, long defaultValue) {
        Properties props = loadProperties();
        return props.getValue(key, defaultValue);
    }

    public static float getProperty(String key, float defaultValue) {
        Properties props = loadProperties();
        return props.getValue(key, defaultValue);
    }

    public static double getProperty(String key, double defaultValue) {
        Properties props = loadProperties();
        return props.getValue(key, defaultValue);
    }


    public String getValue(String key) {
        if (this.properties == null) {
            return null;
        } else {
            return this.properties.getProperty(key);
        }
    }

    public String getValue(String key, String defaultValue) {
        if (this.properties == null) {
            return defaultValue;
        } else {
            return this.properties.containsKey(key) ? this.properties.getProperty(key) : defaultValue;
        }
    }

    public boolean getValue(String key, boolean defaultValue) {
        if (this.properties == null) {
            return defaultValue;
        } else {
            return this.properties.containsKey(key) ? Boolean.parseBoolean(this.properties.getProperty(key)) : defaultValue;
        }
    }

    public byte getValue(String key, byte defaultValue) {
        if (this.properties == null) {
            return defaultValue;
        } else {
            return this.properties.containsKey(key) ? Byte.parseByte(this.properties.getProperty(key)) : defaultValue;
        }
    }

    public short getValue(String key, short defaultValue) {
        if (this.properties == null) {
            return defaultValue;
        } else {
            return this.properties.containsKey(key) ? Short.parseShort(this.properties.getProperty(key)) : defaultValue;
        }
    }

    public int getValue(String key, int defaultValue) {
        if (this.properties == null) {
            return defaultValue;
        } else {
            return this.properties.containsKey(key) ? Integer.parseInt(this.properties.getProperty(key)) : defaultValue;
        }
    }

    public long getValue(String key, long defaultValue) {
        if (this.properties == null) {
            return defaultValue;
        } else {
            return this.properties.containsKey(key) ? Long.parseLong(this.properties.getProperty(key)) : defaultValue;
        }
    }

    public float getValue(String key, float defaultValue) {
        if (this.properties == null) {
            return defaultValue;
        } else {
            return this.properties.containsKey(key) ? Float.parseFloat(this.properties.getProperty(key)) : defaultValue;
        }
    }

    public double getValue(String key, double defaultValue) {
        if (this.properties == null) {
            return defaultValue;
        } else {
            return this.properties.containsKey(key) ? Double.parseDouble(this.properties.getProperty(key)) : defaultValue;
        }
    }

    public java.util.Properties getProperties() {
        return properties;
    }

    public void setProperties(java.util.Properties properties) {
        this.properties = properties;
    }
}
